package com.common.allCommon;

import java.util.List;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;

import com.product.android.business.ApplicationVariable;

public class PackUtils {
	
	public static boolean isTopActivity(boolean isOpenCamera) {
		if(isOpenCamera){
			return true;
		}
		Context context=ApplicationVariable.INSTANCE.applicationContext;
		String packageName = context.getPackageName();
		ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
		if (tasksInfo.size() > 0) {
			// 应用程序位于堆栈的顶层
			String topActivityPackageName =tasksInfo.get(0).topActivity.getPackageName(); 
			if (packageName.equals(topActivityPackageName)||"com.android.camera".equals(topActivityPackageName)) {
				return true;
			}
		}
		return false;
	}

	public static boolean hasPackageName(Context mContext ,String packagename){
		PackageInfo packageInfo;
		try {
			packageInfo = mContext.getPackageManager().getPackageInfo(packagename,0);
		} catch (NameNotFoundException e) {
			packageInfo = null;
			e.printStackTrace();
		}
		if (packageInfo == null) {
			return false;
		} else {
			return true;
		}
    }
	
	public static boolean jumpPackage(Context context, String packagename,String classname,String appname){
		if(hasPackageName(context ,packagename)){
//			Intent intent;
//			intent =  new Intent();
//		    intent.setAction("Android.intent.action.MAIN");
//		    intent.setClassName(packagename, classname);   
//		    context.startActivity(intent);
			jumpActivity(context,packagename);
			return true;
		}
		else{
//			ToastUtils.display(context, "未安装"+appname+"请先下载");
			return false;
		}
	}
	
	@TargetApi(Build.VERSION_CODES.CUPCAKE)
	public static void jumpActivity(Context context,String packageName){
		Intent intent=context.getPackageManager().getLaunchIntentForPackage(packageName);
		try {
			PackageInfo  pageageInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
			if(pageageInfo!=null){
//				System.out.println("版本号:"+pageageInfo.versionCode);
			}
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		if(intent != null){
			context.startActivity(intent);
		}
	}
	
	/**
	 * 判断某个activity是否在显示
	 * <br>Created 2014-5-22 下午7:55:09
	 * @param target
	 * @return
	 * @author       cb
	*/
	public static boolean isTopActivity(Class<?> target) {
		Context context=ApplicationVariable.INSTANCE.applicationContext;
		ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
		if (tasksInfo.size() > 0) {
			// 应用程序位于堆栈的顶层
			String acti=tasksInfo.get(0).topActivity.getClassName();
			String tar=target.getName();
			if (acti.equals(tar)){
				return true;
			}
		}
		return false;
	}
}